package com.atguigu.tingshu.user.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 用户账户初始化使用的消息队列配置
 *
 * @author Taeyang
 * @date 2025/4/26
 */
@Configuration
public class UserAccountInitRabbitConfig {

    /**
     * 账户交换机初始化（点对点）
     * 添加 @Bean注解 代码这个交换机在项目运行后会添加到容器中
     *
     * @return 交换机
     */
    @Bean("accountExchange")
    public Exchange accountExchange() {
        return ExchangeBuilder.directExchange("account_exchange").build();
    }


    /**
     * 账户队列
     *
     * @return 队列
     */
    @Bean("accountQueue")
    public Queue accountQueue() {
        return QueueBuilder.durable("account_queue").build();
    }


    /**
     * 队列交换机绑定
     *
     * @param accountExchange
     * @param accountQueue
     * @return
     */
    @Bean
    public Binding accountBinding(@Qualifier("accountExchange") Exchange accountExchange, @Qualifier("accountQueue") Queue accountQueue) {
        return BindingBuilder.bind(accountQueue).to(accountExchange).with("user.init").noargs();
    }
}
